package com.robmayhew.jsc;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Rob
 * Date: 5-Apr-2005
 * Time: 8:23:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class JavaSrcTokenList
    extends ArrayList
{
    public void add(int row, int col, String token, String line, int position)
    {
        row++;
        if(token == null || token.length() == 0)
            return;
        position = position - token.length();
        if(isCamel(token))
        {
            breakUp(token, row, col, line, position);
        }
        else{
           add(new JavaSrcToken(row, col, token, line, position));
        }
    }

    public JavaSrcToken getToken(int i)
    {
        return (JavaSrcToken)get(i);
    }

    private boolean isCamel(String word)
    {
        boolean mix = false;
        boolean lastUp = false;
        boolean allUp = true;
        char[] ca = word.toCharArray();
        for(int i = 0; i < ca.length; i++){
            char c = ca[i];
            if(Character.isUpperCase(c))
            {
                if(lastUp)
                {

                }else{
                    mix = true;
                    lastUp = true;
                }
            }else{
                lastUp = false;
                allUp = false;
            }
        }
        if(allUp)
            return false;
        return mix;
    }

    void breakUp(String camelWord, int row, int col, String line, int position)
    {

        StringBuffer sb = new StringBuffer();
        char[] ca = camelWord.toCharArray();
        int i = 0;
        int offset = 0;
        for(i = 0; i < ca.length; i++)
        {
            char c = ca[i];
            if(Character.isUpperCase(c))
            {
                if(sb.length() > 0)
                {
                    add(new JavaSrcToken(row, col, sb.toString(), line, position + offset));
                    sb = new StringBuffer();
                    offset = i;
                }
            }
            sb.append(c);
        }
        if(sb.length() > 0)
        {
            add(new JavaSrcToken(row, col, sb.toString(), line, position + offset));
        }

    }
}
